using System;
using UnityEngine;

namespace PKGames.AssetLoader
{
	public class DownloadListener : IDownloadListener
	{
		private readonly Action notifyStart;

		private readonly Action<float> notifySuccess;

		private readonly Action<float> notifyError;

		private readonly Action<float> notifyProgress;

		private float _timeToFinishDownload;

		public DownloadListener(Action onStart, Action<float> onSuccess, Action<float> onError, Action<float> onProgress)
		{
			notifyStart = onStart;
			notifySuccess = onSuccess;
			notifyError = onError;
			notifyProgress = onProgress;
		}

		public void NotifyStart()
		{
			notifyStart.Fire();
			_timeToFinishDownload = Time.time;
		}

		public void NotifyFinishedFailed()
		{
			float time = Time.time;
			notifyError.Fire(time - _timeToFinishDownload);
		}

		public void NotifyFinishedSuccess()
		{
			float time = Time.time;
			notifySuccess.Fire(time - _timeToFinishDownload);
		}

		public void NotifyProgress(float progress, int downloadedBytes, int totalBytes)
		{
			notifyProgress.Fire(progress);
		}
	}
}
